let xxplugins = {}
import localStorage from '@/utils/localStorage'

xxplugins.install = function (Vue, vm) {
  const login = function () {
    vm.$route({
      url: '/pages/authguide/index',
      params: {
        role: 'worker',
      },
      type: 'navigateTo',
    })
  }

  const matchRole = function (role) {
    return new Promise((resolve, reject) => {
      if (vm.$store.getters.roles.length == 0) {
        login()
        reject('您还未登录')
      } else {
        if (vm.$store.getters.roles.indexOf(role) > -1) {
          resolve(true)
        } else {
          reject('您没有该操作的权限')
        }
      }
    })
  }

  const alert = function (msg) {
    const _oftype = Object.prototype.toString.call(msg)
    uni.showToast({
      title: _oftype == '[object Object]' ? JSON.stringify(msg) : msg.toString(),
      duration: 2000,
      icon: 'none',
    })
  }

  const confirm = function (msg, title = '') {
    return new Promise((resolve, reject) => {
      uni.showModal({
        title: title,
        content: msg,
        success: function (res) {
          if (res.confirm) {
            resolve()
          } else if (res.cancel) {
            reject()
          }
        },
      })
    })
  }

  const now = function (t = 'dt') {
    if (t == 'dt') {
      return uni.$u.date(new Date(), 'yyyy-mm-dd hh:MM:ss')
    } else {
      return uni.$u.date(new Date(), 'yyyy-mm-dd')
    }
  }

  const open = function (_url, _params = {}) {
    this.$route({
      url: _url.startsWith('/') ? _url : `/${_url}`,
      params: _params,
      type: 'navigateTo',
    })
  }

  const openB = function (_url, _params = {}) {
    this.$route({
      url: _url,
      params: _params,
      type: 'redirectTo',
    })
  }

  const back = function (_url, _params = {}) {
    uni.navigateBack()
  }

  Vue.prototype.$confirm = confirm
  Vue.prototype.$matchRole = matchRole
  Vue.prototype.$alert = alert
  Vue.prototype.$login = login
  Vue.prototype.$now = now
  Vue.prototype.$http = uni.$u.http
  Vue.prototype.$route = uni.$u.route
  Vue.prototype.$ls = localStorage
  Vue.prototype.$open = open
  Vue.prototype.$openB = openB
  Vue.prototype.$back = back
}
export default xxplugins
